// pages/user/user-message/index.js
const app = getApp()
import { request } from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isOnLoad: false,
    userInfo: null,
    personIsComplete:true, // 个人信息是否完善
    IDImageIsComplete:true, // 身份证是否完善
    papersImageIsComplete: true, // 证件照是否完善
    graduationImageIsComplete: true, // 毕业证是否完善
    is_new: app.globalData.is_new
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      isOnLoad:true
    })
    this.personIsComplete()
    console.log('onLoad');
  },
  // 判断用户信息是否完整
  personIsComplete() {
    request('/api/user/info', app.globalData.token).then(res => {

    this.setData({
      userInfo: res.data
    })
    if(!res.data) {
      this.setData({
        is_new: 0
      })
      app.globalData.is_new = 0
      app.globalData.userInfo = res.data
      return 
    }
    console.log(this.data.userInfo, 'this.data.userInfo');
    app.globalData.userInfo = res.data
    let personIsComplete=true,
    IDImageIsComplete=true,
    papersImageIsComplete=true,
    graduationImageIsComplete=true
    let personMessage = [
      'gender',
      'nation',
      'hk_address',
      'name',
      'phone',
      'IDcard',
      'education_bg',
      'graduationSchool',
      'graduationProfession',
      'graduationTime',
      'graduationID',
      'exam_address',
      'school',
      'profession',
      'level',
      'take_info'
    ]
    // 判断个人信息是否完善
    for (const item of personMessage) {
      // if(true) return false 
      if(!this.data.userInfo[item])  {
        personIsComplete = false;
        break;
      }
    }
    // 判断身份证是否完善
    if(!this.data.userInfo.ID_image || !this.data.userInfo.ID_back_image) IDImageIsComplete = false
    // 判断证件照是否完善
    if(!this.data.userInfo.papers_image || !this.data.userInfo.person_image) papersImageIsComplete = false
    // 判断毕业证是否完善
    if(!this.data.userInfo.graduation_image) graduationImageIsComplete = false
    this.setData({
      personIsComplete,
      IDImageIsComplete,
      papersImageIsComplete,
      graduationImageIsComplete
    })
  })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(!this.data.isOnLoad) this.personIsComplete()
    this.setData({
      isOnLoad:false
    })
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})